﻿/*
-----------------------------------------------------------------------------
This source file is part of Cell Cloud.

Copyright (c) 2009-2012 Cell Cloud Team - cellcloudproject@gmail.com

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-----------------------------------------------------------------------------
*/

#ifndef CellCryptology_h
#define CellCryptology_h

#include "CellPrerequisites.h"
#include "CellSingleton.h"

namespace cell
{
/** \addtogroup Core
 *  @{
 */
/** \addtogroup General
 *  @{
 */

class CryptologyImpl;
template class _CellExport boost::scoped_ptr<CryptologyImpl>;

/** 加解密工具类。
@author
	Xu Jiangwei
*/
class _CellExport Cryptology : public Singleton<Cryptology>,
	public GeneralAlloc
{
public:
	/** 默认构造函数。
	*/
	Cryptology(void);

private:
	/**
	*/
	Cryptology(const Cryptology& other);

public:
	/** 析构函数。
	*/
	virtual ~Cryptology(void);

public:
	/**
	*/
	typedef enum _hash
	{
		MD5,
		SHA_1
	} hash_t;

public:
	/** 简单数据异或。密钥长度必须为 8 位。
	*/
	int simpleEncrypt(char* out, const char* text, int length, const char* key);

	/** 简单数据异或。密钥长度必须为 8 位。
	*/
	int simpleDecrypt(char* out, const char* text, int length, const char* key);

	/**
	*/
	int beginHash(hash_t hash);

	/**
	*/
	void updateHash(int sn, byte* in, size_t length);

	/**
	*/
	int endHash(byte* out, int sn);

public:
	/**
	*/
	static String bytesToString(const byte* bytes, int length);

public:
	/** 覆盖标准 Singleton 检索方法。
	*/
	static Cryptology& getSingleton(void);

	/** 覆盖标准 Singleton 检索方法。
	*/
	static Cryptology* getSingletonPtr(void);

private:
	boost::scoped_ptr<CryptologyImpl> _impl;
};

/** @} */
/** @} */
} // end namespace cell

#endif // CellCryptology_h
